package com.xmtlxx.tms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.tms.domain.Carrier;
import com.xmtlxx.tms.domain.bo.CarrierBo;
import com.xmtlxx.tms.domain.vo.CarrierVo;
import com.xmtlxx.tms.mapper.CarrierMapper;
import com.xmtlxx.tms.service.ICarrierService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmtlxx.system.mapper.SysDeptMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 承运商Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class CarrierServiceImpl implements ICarrierService {

    private final CarrierMapper baseMapper;

    private final SysDeptMapper deptMapper;

    /**
     * 查询承运商
     *
     * @param id 承运商主键
     * @return 承运商
     */
    @Override
    public CarrierVo queryById(String id){
        //return baseMapper.selectVoById(id);
        return baseMapper.selectCarrierVoByID(id);
    }

    /**
     * 查询承运商列表
     *
     * @param bo 承运商
     * @return 承运商
     */
    @Override
    public TableDataInfo<CarrierVo> queryPageList(CarrierBo bo, PageQuery pageQuery) {
       // LambdaQueryWrapper<Carrier> lqw = buildQueryWrapper(bo);
       // Page<CarrierVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<CarrierVo> result = baseMapper.selectPageCarrierList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询承运商列表
     *
     * @param bo 承运商
     * @return 承运商
     */
    @Override
    public List<CarrierVo> queryList(CarrierBo bo) {
       // LambdaQueryWrapper<Carrier> lqw = buildQueryWrapper(bo);
       // return baseMapper.selectVoList(lqw);
        return baseMapper.selectCarrierList(bo);
    }

    private LambdaQueryWrapper<Carrier> buildQueryWrapper(CarrierBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Carrier> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, Carrier::getDeptId, bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getDeptName()), Carrier::getDeptName, bo.getDeptName());
        lqw.eq(StringUtils.isNotBlank(bo.getContacts()), Carrier::getContacts, bo.getContacts());
        lqw.eq(StringUtils.isNotBlank(bo.getContactsTel()), Carrier::getContactsTel, bo.getContactsTel());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), Carrier::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getCarrierType()), Carrier::getCarrierType, bo.getCarrierType());
        lqw.eq(bo.getBingUserId() != null, Carrier::getBingUserId, bo.getBingUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), Carrier::getCreateBy, bo.getCreateBy());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            Carrier::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), Carrier::getDelFlag, bo.getDelFlag());
        return lqw;
    }

    /**
     * 新增承运商
     *
     * @param bo 承运商
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(CarrierBo bo) {
        Carrier add = BeanUtil.toBean(bo, Carrier.class);
        validEntityBeforeSave(add);

        //填充UUID
        add.setId(IdUtil.simpleUUID());
        add.setDelFlag("0");
        add.setDeptName(deptMapper.selectVoById(add.getDeptId()).getDeptName());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改承运商
     *
     * @param bo 承运商
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(CarrierBo bo) {
        Carrier update = BeanUtil.toBean(bo, Carrier.class);
        validEntityBeforeSave(update);
        update.setDeptName(deptMapper.selectVoById(update.getDeptId()).getDeptName());
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(Carrier entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除承运商
     *
     * @param ids 需要删除的承运商主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public CarrierVo selectCarrierByDeptId(Long deptId) {
        return baseMapper.selectVoOne(new LambdaQueryWrapper<Carrier>().eq(Carrier::getDeptId,deptId));
    }
}
